﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Agr.CQRS.Domain;
using Agr.CQRS.Domain.Events;

namespace Tests.Agr.CQRS.Domain
{
    [TestClass]
    public class When_AggregateRoot_Handles_A_DomainEvent
    {
        private aggrRootImpl target;

        [TestInitialize]
        public void TestInit()
        {
            //Given
            target = new aggrRootImpl();

            //When
            target.Aggregate.Handle(new domainEventImpl() { Entitynumber = 0 });
            target.Aggregate.Handle(new domainEventForEntityImpl() { Entitynumber = 1 });
        }


        [TestMethod()]
        public void Then_Method_With_Event_Signature_Is_Called()
        {
            Assert.IsTrue(target.HandledEvent);
        }

        [TestMethod()]
        public void Then_The_Events_Are_Saved_In_The_UncommittedEvents()
        {
            Assert.AreEqual(2, target.Aggregate.UnCommittedEvents.Count);
        }

        [TestMethod()]
		public void Then_SubEntities_Handles_Its_Own_Event()
		{
            Assert.IsTrue(target.EntityInAggregate.HandledEvent);
		}
        
        private class aggrRootImpl : AggregateRoot
        {
            public entityImpl EntityInAggregate;
            public aggrRootImpl()
            {
                EntityInAggregate = new entityImpl();
                Aggregate.RegisterEntity(EntityInAggregate);
            }

            public bool HandledEvent { get; set; }
            private void handleDomainEventImpl(domainEventImpl ev)
            {
                HandledEvent = true;
            }
        }
        private class entityImpl : Entity
        {
            public bool HandledEvent { get; set; }
            private void handleDomainEventForEntityImpl(domainEventForEntityImpl ev)
            {
                HandledEvent = true;
            }
        }

        private class domainEventImpl : DomainEvent
        {
        }

        private class domainEventForEntityImpl : DomainEvent
        {
        }
    }
}
